package frame;

import MyBatis.MyBatislianjian;
import constant.Constant;
import org.apache.ibatis.session.SqlSession;
import org.example.mapper.EmployeeMapper;
import org.example.pojo.Employee;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;

public class LandPanel extends JPanel {
    private BufferedImage backgroundImage;

    private SqlSession sqlSession = MyBatislianjian.getSqlSession();
    private static final int ARC_WIDTH = 80; // 圆角宽度
    private static final int ARC_HEIGHT = 80; // 圆角高度
    private static String x;

    private JPanel panel; // 定义为类成员变量以便在调整时访问
    private JButton loginButton; // 将登录按钮设为类成员变量以便在回车事件中访问
    private JRadioButton adminRadio; // 管理员单选按钮
    private JRadioButton employeeRadio; // 员工单选按钮
    private ButtonGroup group; // 单选按钮组

    public LandPanel() {
        this.setBounds(0, 0, Constant.width, Constant.height);
        this.setLayout(null); // 使用绝对布局

        // 加载主背景图片
        try {
            InputStream imageStream = getClass().getClassLoader().getResourceAsStream("images/chaoshi.jpg");
            if (imageStream != null) {
                backgroundImage = ImageIO.read(imageStream);
            } else {
                System.err.println("未找到图片: images/chaoshi.jpg");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 创建并添加内层面板
        panel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                // 绘制圆角矩形背景
                g2d.setColor(new Color(255, 255, 255, 150)); // 半透明背景
                g2d.fillRoundRect(0, 0, getWidth(), getHeight(), ARC_WIDTH, ARC_HEIGHT); // 绘制圆角矩形

                g2d.dispose();
            }
        };
        panel.setBounds(220, 100, 460, 300); // 初始大小和位置
        panel.setLayout(null); // 使用绝对布局
        panel.setOpaque(false); // 设置为透明背景
        this.add(panel);

        // 创建标题标签
        JLabel titleLabel = new JLabel("超市管理系统");
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 40));
        titleLabel.setBounds(110, 10, 300, 50);
        titleLabel.setForeground(Color.BLACK); // 设置标题颜色
        panel.add(titleLabel);

        // 创建账号标签
        JLabel accountLabel = new JLabel("账 号：");
        accountLabel.setFont(new Font("微软雅黑", Font.BOLD, 20));
        accountLabel.setBounds(80, 100, 80, 30);
        accountLabel.setForeground(Color.BLACK); // 设置标签颜色
        panel.add(accountLabel);

        JTextField accountTextField = new JTextField(13);
        accountTextField.setFont(new Font("微软雅黑", Font.PLAIN, 20));
        accountTextField.setBounds(160, 100, 200, 30);
        accountTextField.setOpaque(false); // 设置文本框透明
        accountTextField.setBackground(new Color(255, 255, 255, 150)); // 半透明背景
        accountTextField.setBorder(BorderFactory.createLineBorder(Color.BLACK)); // 设置黑色边框
        panel.add(accountTextField);

        // 创建密码标签
        JLabel passwordLabel = new JLabel("密 码：");
        passwordLabel.setFont(new Font("微软雅黑", Font.BOLD, 20));
        passwordLabel.setBounds(80, 150, 80, 30);
        passwordLabel.setForeground(Color.BLACK); // 设置标签颜色
        panel.add(passwordLabel);

        JPasswordField passwordField = new JPasswordField(13);
        passwordField.setFont(new Font("微软雅黑", Font.PLAIN, 20));
        passwordField.setBounds(160, 150, 200, 30);
        passwordField.setOpaque(false); // 设置密码框透明
        passwordField.setBackground(new Color(255, 255, 255, 150)); // 半透明背景
        passwordField.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        panel.add(passwordField);

        // 创建按钮
        loginButton = new JButton("登录");
        loginButton.setFont(new Font("微软雅黑", Font.BOLD, 20));
        loginButton.setBounds(110, 220, 100, 40);
        // 按钮透明处理
        loginButton.setOpaque(false);
        loginButton.setContentAreaFilled(false);
        loginButton.setBorderPainted(false);
        loginButton.setForeground(Color.BLACK); // 设置按钮文字颜色
        panel.add(loginButton);
        // 创建退出系统按钮
        JButton exitButton = new JButton("退出系统");
        exitButton.setFont(new Font("微软雅黑", Font.BOLD, 20));
        exitButton.setBounds(230, 220, 100, 40);
        exitButton.setOpaque(false);
        exitButton.setContentAreaFilled(false);
        exitButton.setBorderPainted(false);
        exitButton.setForeground(Color.BLACK);
        panel.add(exitButton);


        // 登录按钮点击事件
        loginButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent event) {
                handleLogin(accountTextField.getText(), new String(passwordField.getPassword()));
            }
        });
        // 添加退出按钮点击事件
        exitButton.addActionListener(e -> {
            int option = JOptionPane.showConfirmDialog(null, "确定要退出系统吗？", "退出", JOptionPane.YES_NO_OPTION);
            if (option == JOptionPane.YES_OPTION) {
                System.exit(0); // 关闭程序
            }
        });


        // 为账号文本框和密码框添加键盘事件监听器
        KeyAdapter enterKeyListener = new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    handleLogin(accountTextField.getText(), new String(passwordField.getPassword()));
                }
            }
        };
        accountTextField.addKeyListener(enterKeyListener);
        passwordField.addKeyListener(enterKeyListener);

// 创建管理员和员工单选按钮
        adminRadio = new JRadioButton("管理员");
        adminRadio.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        adminRadio.setBounds(120, 190, 100, 30);
        adminRadio.setOpaque(false); // 设置为透明
        panel.add(adminRadio);

        employeeRadio = new JRadioButton("员工");
        employeeRadio.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        employeeRadio.setBounds(240, 190, 100, 30);
        employeeRadio.setOpaque(false); // 设置为透明
        panel.add(employeeRadio);

        // 创建按钮组，确保单选按钮互斥
        group = new ButtonGroup();
        group.add(adminRadio);
        group.add(employeeRadio);

        // 监听父容器大小变化事件
        this.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
                adjustPanelPosition();
            }
        });


        adjustPanelPosition(); // 初始调整位置

    }

    // 调整登录面板位置方法
    private void adjustPanelPosition() {
        // 获取父容器的宽度和高度
        int parentWidth = getWidth();
        int parentHeight = getHeight();

        // 计算新的面板位置
        int panelWidth = panel.getWidth();
        int panelHeight = panel.getHeight();
        int newX = (parentWidth - panelWidth) / 2;
        int newY = (parentHeight - panelHeight) / 2;

        // 设置面板的新位置
        panel.setLocation(newX, newY);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (backgroundImage != null) {
            g.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), this);
        } else {
            g.setColor(Color.LIGHT_GRAY);
            g.fillRect(0, 0, getWidth(), getHeight());
        }
    }

    // 登录处理方法
    private void handleLogin(String account, String password) {
        x=account;
        EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
        Employee employee = employeeMapper.selectEmployeeByEid(account);

        if (employee == null) {
            JOptionPane.showMessageDialog(null, "登录失败，请检查账号和密码！", "错误", JOptionPane.ERROR_MESSAGE);
        } else {
            if (employee.getEid().equals(account) && employee.getEPas().equals(password)) {
                // 登录成功
                if (adminRadio.isSelected()) {
                    // 管理员登录
                    if(employee.getElevel().equals("00"))
                    {
                        JOptionPane.showMessageDialog(null, "管理员登录成功！", "提示", JOptionPane.INFORMATION_MESSAGE);
                        // 跳转到管理员界面
                        AppFrame.setContent(new GuanliPanel());
                    }
                    else
                    {
                        //弹窗提示账号密码
                        JOptionPane.showMessageDialog(null, "登录失败，请检查账号和密码！", "错误", JOptionPane.ERROR_MESSAGE);
                    }
                } else if (employeeRadio.isSelected()) {
                    // 普通员工登录
                    if(employee.getElevel().equals("11"))
                    {
                        JOptionPane.showMessageDialog(null, "普通员工登录成功！", "提示", JOptionPane.INFORMATION_MESSAGE);
                        // 这里可以根据需求进行普通员工的界面跳转或其他处理
                        AppFrame.setContent(new yuangongPabel());
                    }
                    else
                    {
                        JOptionPane.showMessageDialog(null, "登录失败，请检查账号和密码！", "错误", JOptionPane.ERROR_MESSAGE);
                    }

                } else {
                    // 未选择角色
                    JOptionPane.showMessageDialog(null, "请选择登录角色！", "提示", JOptionPane.WARNING_MESSAGE);
                }
            } else {
                // 密码错误
                JOptionPane.showMessageDialog(null, "登录失败，请检查账号和密码！", "错误", JOptionPane.ERROR_MESSAGE);
            }
        }
    }


    public static String[] getyonghu() {
        String[] s = {x};
        return s;
    }

    public static String getyonghu1() {
        return x;
    }
}
